package com.team.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;

import com.team.dao.EquipmentDao;
import com.team.entity.Equipment;

@Repository
public class EquipmentDaoImpl implements EquipmentDao {
	@Resource
	private SessionFactory sessionFactory;

	
	public Equipment selectEquipmentById(int id) {
		try {
			Equipment equipment = this.sessionFactory.getCurrentSession().get(Equipment.class, id);
			return equipment;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	
	public void saveEquipment(Equipment equipment) {
		try {
			this.sessionFactory.getCurrentSession().save(equipment);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	
	public void deleteEquipment(Integer id) {
		try {
			Session session = this.sessionFactory.getCurrentSession();
			Equipment equipment = session.get(Equipment.class, id);
			session.delete(equipment);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	
	public void updateEquipment(Equipment equipment) {
		try {
			this.sessionFactory.getCurrentSession().update(equipment);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	
	public List<Equipment> selectAllEquipment(int pagenumber) {
		String hql = "from Equipment";
		try {
			Query query = this.sessionFactory.getCurrentSession().createQuery(hql);
			query.setFirstResult(pagenumber*5);
			query.setMaxResults(5);
			List<Equipment> list = query.list();
			return list;
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
